﻿using System;
using System.Linq;
using System.Reactive.Concurrency;
using System.Reactive.Linq;
using System.Windows;

namespace Present.Commands.Rx
{
    public static class UiObservableExtensions
    {
        public static IObservable<T> ObserveOnUiThread<T>(this IObservable<T> me)
        {
            return me.ObserveOn(SchedulerFactory.UiScheduler);
        }
    }

    /// <summary>
    /// Creates a Scheduler for the UI, defaulting to Scheduler.Dispatcher.
    /// Here so we can test.
    /// </summary>
    public static class SchedulerFactory
    {

        static SchedulerFactory()
        {
            UiSchedulerCreator = () => new DispatcherScheduler(Application.Current.Dispatcher);
        }

        private static IScheduler uiScheduler;

        public static IScheduler UiScheduler
        {
            get { return uiScheduler ?? (uiScheduler = UiSchedulerCreator()); }
        }

        public static Func<IScheduler> UiSchedulerCreator { get; set; }
    }
}